<template>
  <div class="roles-container">
      <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcom' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- card区域 -->
    <el-card class="box-card">
      <!-- 添加角色 -->
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <!-- 展示角色列表 -->
       <el-table :data="rolesList" border stripe>
         <!-- 展开列 -->
          <el-table-column type="expand">
            <template slot-scope="scope">
              <el-row :class="['bdbottom', i1 === 0 ? 'bdtop' : '','vcenter']" v-for="(item1,i1) in scope.row.children" :key="item1.id">
                <!-- 渲染一级权限 -->
                <el-col :span="5">
                  <el-tag closable @close="removeRightById(scope.row,item1.id)">{{item1.authName}}</el-tag>
                   <i class="el-icon-caret-right"></i>
                  </el-col>
                <!-- 渲染二级和三级权限 -->
                <el-col :span="19">
                  <el-row :class="[i2 === 0 ? '' : 'bdtop','vcenter']" v-for="(item2,i2) in item1.children" :key="item2.id">
                    <el-col :span="6">
                      <el-tag type="success" closable @close="removeRightById(scope.row,item2.id)">{{item2.authName}}</el-tag>
                      <i class="el-icon-caret-right"></i>
                    </el-col>
                    <el-col :span="18">
                      <el-tag type="warning" v-for="(item3,id) in item2.children" :key="id" closable @close="removeRightById(scope.row,item3.id)">{{item3.authName}}</el-tag>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
              <!-- <pre>
                {{scope.row}}
              </pre> -->
            </template>
          </el-table-column>
          <!-- 索引列 -->
          <el-table-column type="index" label="#" width="100"  align="center"></el-table-column>
          <el-table-column label="角色名称" prop="roleName"  align="center"></el-table-column>
          <el-table-column label="角色描述" prop="roleDesc"  align="center"></el-table-column>
          <el-table-column label="操作" width="300" align="center">
             <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" size="small">编辑</el-button>
                <el-button type="danger" icon="el-icon-delete" size="small">删除</el-button>
                <el-button type="warning" icon="el-icon-setting" size="small" @click="showSetRightDialog(scope.row)">分配权限</el-button>
             </template>
          </el-table-column>
       </el-table>
    </el-card>
    <!-- 设置分配权限对话框 -->
    <el-dialog
        title="分配权限"
        :visible.sync="setRightDialogVisible"
        width="50%"
        @close="setRightDialogKeys"
        >
        <el-tree :data="rightsList" show-checkbox node-key="id" default-expand-all :default-checked-keys="defKeys" :props="treeProps" ref="treeRef"></el-tree>
        <span slot="footer" class="dialog-footer">
      <el-button @click="setRightDialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="allotRights">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
export default {
  data(){
    return{
    //用户角色列表
      rolesList:[],
      setRightDialogVisible:false,
      rightsList:[],
      //树形控件绑定对象
      treeProps:{
        label: 'authName',
        children: 'children'
      },
      //默认选中的属性checkbox
      defKeys:[],
      //当前即将分配权限的角色id
      roleId:""
    }
  },
  created(){
    //用户角色事件
    this.getroles();
  },
  methods:{
    //获取用户角色
    getroles(){
      this.$http.get("roles").then((res)=>{
        this.rolesList = res.data.data;
      }).catch((err)=>{
        throw err
        console.log(err)
      })
    },
    //根据id删除对应权限
    removeRightById(role,rightId){
      //提示弹框
      this.$confirm("是否删除此权限, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          //执行删除接口请求
          this.$http.delete(`roles/${role.id}/rights/${rightId}`).then(res => {
            //为了避免删除后关闭窗口重新加载，可以直接把后台请求的数据给这个本组件
            role.children = res.data.data;
          });
          this.$message({
            type: "success",
            message: "删除成功"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //设置分配权限方法
    showSetRightDialog(role){
      this.roleId = role.id;
      //获取所有权限
      this.$http.get("rights/tree").then((res)=>{
        console.log(res.data.data)
        this.rightsList = res.data.data;
         this.setRightDialogVisible = true;
         this.getLeafKyes(role,this.defKeys)
      }).catch((err)=>{
      })
    },
    //通过递归获取角色3级权限id
    getLeafKyes(node,arr){
      if(!node.children){
        return arr.push(node.id)
      }
      node.children.forEach(item => {
        this.getLeafKyes(item,arr)
      });
    },
    //清空keys的回调函数事件
    setRightDialogKeys(){
      this.defKeys = [];
    },
    //点击确定分配权限
    allotRights(){
      const keys = [...this.$refs.treeRef.getCheckedKeys(),...this.$refs.treeRef.getHalfCheckedKeys()]
      const idStr = keys.join(",")
      this.$http.post(`roles/${this.roleId}/rights`,{rids:idStr}).then((res)=>{
        this.$message.success("分配权限成功")
        this.getroles();
        this.setRightDialogVisible = false;
      }).catch((err)=>{
        return this.$message.error("分配权限失败!")
      })
    }
  }
}
</script>

<style lang="less" scoped>
.el-tag{
  margin: 10px;
}
.bdtop{
  border-top: 1px solid #eee;
}
.bdbottom{
  border-bottom: 1px solid #eee;
}
.vcenter{
  display: flex;
  align-items: center;
}
</style>>